import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import axios from "axios" 
export default new Vuex.Store({
  state: {
      list:[],
      listArr:[],
      copyid:"",
      copylist:[]
  },
  getters:{
    getxb:(state)=>(v)=>{
      state.listArr=state.list[v].children
      state.copylist=state.list[v].children
    },
    getadd:(state)=>(id)=>{
       state.listArr.forEach((item,index)=>{
         if(item.id==id){
           state.listArr.splice(item,1)
         }
       })
    }
  },
  mutations: {
      setlist(state,payload){
        state.list=payload.data
        state.copylist=payload.data[0].children
        state.listArr=payload.data[0].children
      },
      setcopyid(state,payload){
        state.copyid=payload
        // console.log(state.copyid);
      },
      setxg(state,payload){
        // console.log(payload);
        state.listArr.forEach((item,index)=>{
          if(item.id==state.copyid){
            item.name=payload
          }
        })
      },
      setsc(state,payload){
        state.listArr=state.copylist.filter(item=>item.name.includes(payload))
      }
  },
  actions: {
      getlist({commit}){
        axios.get("/list").then(res=>{
          commit("setlist",res)
        })
      }
  },
  modules: {
  }
})
